<%= page_title(@cluster.new_record? ? "New Kubernetes Cluster" : "Edit Kubernetes Cluster") %>
<% config_contexts = (@cluster.config_filepath ? @cluster.kubeconfig.contexts : []) %>

<section>
  <%= form_for @cluster, html: { class: "form-horizontal" } do |form| %>
    <%= render 'shared/errors', object: @cluster %>

    <fieldset>
      <%= form.input :name, required: true %>
      <%= form.input :description %>
      <%= form.input :config_filepath, label: 'Config File path', pattern: /\A\/.*\z/, help: 'Absolute paths only', required: true %>
      <%= form.input :config_context, label: 'Context', required: true do %>
        <% if config_contexts.any? %>
          <%= form.select :config_context, config_contexts, {}, { class: 'form-control' } %>
        <% else %>
          <%= form.text_field :config_context, class: "form-control" %>
        <% end %>
      <% end %>
      <%= form.input :ip_prefix,
          label: "IP prefix",
          pattern: Kubernetes::Cluster::IP_PREFIX_PATTERN,
          help: "First 1 to 3 sections of an IPv4 address to replace Service clusterIP, for example 123.231"
      %>

      <%= form.actions delete: true do %>
        <% if Samson::Hooks.active_plugin?('aws_ecr') && SamsonAwsEcr::Engine.active? && @cluster.persisted? %>
          <%= link_to "Seed ECR", seed_ecr_kubernetes_cluster_path(@cluster), class: "btn btn-default", data: {method: :post} %>
        <% end %>
      <% end %>
    </fieldset>
  <% end %>
</section>

<% if config_contexts.any? %>
  <script>
    // When an existing config is changed, we have to allow editing the context
    // since we do not know what contexts are in the new file
    $('#kubernetes_cluster_config_filepath').one('keyup', function(){
      var context = $('#kubernetes_cluster_config_context');
      context.replaceWith('<input name="' + context.attr('name') + '" type="text" value="' + context.val() + '" class="form-control">');
    });
  </script>
<% end %>
